(function() {

	var app = angular.module('babyApp');

	app.controller('SaveController', [ '$scope', '$http', 'dateFilter', /*'FetchController',*/ function($scope, $http, dateFilter/*, FetchController*/) {

		var now = new Date();
		$scope.newLog = {
			"timestamp" : now,
			"leftBreast" : 15,
			"rightBreast" : 20,
			"pumped" : 30,
			"formula" : 30,
			"diaper" : "WD",
			"updateUser" : "Amruta [ng]",
			"updateTimestamp" : now
		};

		// /////////////////////////////////////////////

		$scope.newLog.timestamp = new Date();
		$scope.timestampString = dateFilter($scope.newLog.timestamp, "MM-dd-yyyy");

		$scope.$watch('newLog.timestamp', function(timestamp) {
			$scope.timestampString = dateFilter(timestamp, "MM-dd-yyyy");
		});

		$scope.$watch('timestampString', function(timestampString) {

			if (angular.isDate(timestampString)) {
				$scope.newLog.timestamp = new Date(timestampString);
			}/* else {
				$scope.newLog.timestamp = new Date();
			}*/
		});

		// /////////////////////////////////////////////
		$scope.saveLog = function() {

			delete $http.defaults.headers.common['X-Requested-With'];

			// var localAddUrl = 'http://localhost:7001/baby-rest-services/log/add';
			var localAddUrl = 'http://localhost:5555/log/add';
			// var localAddUrl = 'http://default-environment-2xgz38vab8.elasticbeanstalk.com/log/add';

			$http.post(localAddUrl, angular.toJson($scope.newLog)) //
			.success(function(data, status, headers, config) {

				alert('.. added ..');

			}).error(function(data, status, headers, config) {

				alert('Error [' + status + ']');

			});
		}

	} ]);

})();